package com.gzy.xuexiziliao.controller;

import com.gzy.xuexiziliao.common.result.PageResult;
import com.gzy.xuexiziliao.common.result.Result;
import com.gzy.xuexiziliao.pojo.entity.Admin;
import com.gzy.xuexiziliao.service.AdMinService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 *管理员相关接口
 */
@Slf4j
@RestController
@RequestMapping("/admin/admins")
public class AdminController {
    @Autowired
    private AdMinService adminService;

    @PostMapping
    public Result save(@RequestBody Admin admin){
        log.info("新增管理员,admin:{}",admin);
        adminService.save(admin);
        return Result.success("添加成功");
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("批量删除，ids:{}",ids);
        adminService.delete(ids);
        return Result.success("删除成功");
    }
    @PutMapping
    public Result update(@RequestBody  Admin admin){
        log.info("更新员工:{}",admin);
        adminService.update(admin);
        return Result.success("更新成功");
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据id查询管理员：{}",id);
        Admin admin =adminService.getById(id);
        return Result.success(admin);
    }
    @GetMapping("/name")
    public Result<List<Admin>> getByName(@RequestParam String name){
        log.info("根据name查询管理员：{}",name);
        List<Admin> admin =adminService.getByName(name);
        if (admin == null){
            return Result.error("未找到该用户");
        }
        return Result.success(admin);
    }

    @GetMapping("/list")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end){
        log.info("分页查询参数：{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);
        PageResult pageResult=adminService.page(page,pageSize,name,gender,begin,end);
        return Result.success(pageResult);
    }

}








